package cn.msqweb.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.msqweb.entity.IndexImg;
import cn.msqweb.model.Result;
import cn.msqweb.service.IndexImgService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图业务控制层
 */
@Api(tags = "轮播图接口管理")
@RequestMapping("admin/indexImg")
@RestController
public class IndexImgController {

    @Autowired
    private IndexImgService indexImgService;


    @ApiOperation("多条件分页查询轮播图列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public Result<Page<IndexImg>> loadIndexImgPage(@ApiParam("页码") @RequestParam Long current,
                                                   @ApiParam("每页显示条数") @RequestParam Long size,
                                                   @ApiParam("状态") @RequestParam(required = false) Integer status) {
        // 创建分页对象
        Page<IndexImg> page = new Page<>(current,size);
        // 多条件分页查询轮播图
        page = indexImgService.page(page,new LambdaQueryWrapper<IndexImg>()
                        .eq(ObjectUtil.isNotNull(status),IndexImg::getStatus,status)
                .orderByDesc(IndexImg::getSeq)
        );
        return Result.success(page);
    }

    @ApiOperation("新增轮播图")
    @PostMapping
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public Result<String> saveIndexImg(@ApiParam("轮播图对象") @RequestBody IndexImg indexImg) {
        Boolean saved = indexImgService.saveIndexImg(indexImg);
        return Result.handler(saved);
    }

    @ApiOperation("根据标识查询轮播图信息")
    @GetMapping("info/{imgId}")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public Result<IndexImg> loadIndexImgInfo(@ApiParam("轮播图标识") @PathVariable Long imgId) {
        IndexImg indexImg = indexImgService.queryIndexImgInfoById(imgId);
        return Result.success(indexImg);
    }

    @ApiOperation("修改轮播图信息")
    @PutMapping
    @PreAuthorize("hasAuthority('admin:indexImg:update')")
    public Result<String> modifyIndexImg(@ApiParam("轮播图对象") @RequestBody IndexImg indexImg) {
        boolean updated = indexImgService.updateById(indexImg);
        return Result.handler(updated);
    }


    @ApiOperation("批量删除轮播图")
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin:indexImg:delete')")
    public Result<String> removeIndexImg(@ApiParam("轮播图id集合") @RequestBody List<Long> ids) {
        boolean removed = indexImgService.removeBatchByIds(ids);
        return Result.handler(removed);
    }
}
